/*
 * 文件名：ProContractController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：chenxiaoyong
 * 修改时间：2020年1月15日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.bpa.domain.ProContractAssist;
import top.infopub.mgr.bpa.domain.ProContractDomain;
import top.infopub.mgr.bpa.service.InnerAgrtDetailService;
import top.infopub.mgr.bpa.service.ProContractService;
import top.infopub.mgr.bpa.service.SubpackCotDetailService;
import top.infopub.mgr.bpa.vo.ProContractSubVo;
import top.infopub.mgr.bpa.vo.ProContractVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.domain.OrgInfoDomain;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.service.AfterSummaryService;
import top.infopub.mgr.mkt.service.DeciProService;
import top.infopub.mgr.mkt.service.DiscloseProService;
import top.infopub.mgr.mkt.service.PreProjectService;
import top.infopub.mgr.mkt.vo.DiscloseProVo;
import top.infopub.mgr.sg.service.CitySurveyService;
import top.infopub.mgr.util.FileHelper;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.bpa.ProContractAtt;
import top.infopub.model.bpa.ProContractDetail;
import top.infopub.model.bpa.ProContractInvest;
import top.infopub.model.bpa.ProContractSub;
import top.infopub.model.bpa.ProContractUla;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.mkt.AfterSummary;
import top.infopub.model.mkt.DeciPro;
import top.infopub.model.mkt.DisclosePro;
import top.infopub.model.sg.CitySurvey;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;


/**对外合同
 * @author chenxiaoyong
 * @version 2020年1月15日
 * @see ProContractController
 * @since商务管理/对外合同/项目合同
 */
@Controller
@RequestMapping("/proContract")
public class ProContractController extends GenericFlowController {
    @Autowired
    private ProContractService proContractService;

    @Autowired
    private PreProjectService preProjectService;

    @Autowired
    private StaffInfoService staffInfoService;

    //交底管理
    @Autowired
    private DiscloseProService discloseProService;

    //公司管理
    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private CitySurveyService citySurveyService;

    @Autowired
    private DeciProService deciProService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private InnerAgrtDetailService innerAgrtDetailService;

    @Autowired
    private SubpackCotDetailService subpackCotDetailService;

    @Autowired
    private AfterSummaryService afterSummaryService;

    /**
     * 项目合同列表
     * @return 
     * @see
     */
    @RequestMapping("/list")
    public String list() {
        return "bpa/lay-procontract-list";
    }

    /**
     * 项目合同列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<ProContractDomain> queryDataByCondition(ProContractVo proContractVo,
                                                         ParamPage paramPage) {
        Page<ProContractDomain> page = new Page<ProContractDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (proContractVo.getEndDate() != null) {
            proContractVo.setEndDate(DateUtils.addDays(proContractVo.getEndDate(), 1));
        }
        page = proContractService.selectByDomainWhere(page, proContractVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 项目合同-我审批的列表数据
     *
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<ProContractDomain> queryMyCheckDataByCondition(ProContractVo proContractVo,
                                                                ParamPage paramPage) {
        Page<ProContractDomain> page = new Page<ProContractDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (proContractVo.getEndDate() != null) {
            proContractVo.setEndDate(DateUtils.addDays(proContractVo.getEndDate(), 1));
        }
        proContractVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = proContractService.selectMyCheckByDomainWhere(page, proContractVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 项目合同-新增
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(HttpServletRequest request) {
        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
        request.setAttribute("bindOrgId", bindOrgId);
        request.setAttribute("bindOrgName", bindOrgName);
        request.setAttribute("bindDeptId", bindDeptId);
        request.setAttribute("bindDeptName", bindDeptName);
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        //返回所有已经交底的项目名称
        List<DisclosePro> discloseProList = discloseProService.selectAllUsableDisclosePro(null,
            null);
        request.setAttribute("discloseProList", discloseProList);
        return "bpa/lay-procontract-add";
    }

    /**
     * 项目合同-新增
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(ProContractAssist proContractAssist) {

        String proCode = "";
        String[] strCodes = proContractAssist.getProCodeAndCodes().split("\\_");
        proCode = strCodes[0];
        //判断项目合同名称是否存在
        List<ProContract> contractName = proContractService.isExistContractName(
            proContractAssist.getContractName(), proCode);
        if (CollectionUtils.isNotEmpty(contractName)) {
            return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("项目合同名称【"
                                                            + proContractAssist.getContractName()
                                                            + "】已经存在~");
        }
        ProContract existProName = proContractService.isExistProName(proCode);
        if (existProName != null) {
            return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("项目名称【" + existProName.getProName()
                                                            + "】已经存在~");
        }
        //主表-项目合同
        proContractAssist.setHrDeptId(proContractAssist.getHrDeptid());
        proContractAssist.setHrOrgId(proContractAssist.getHrOrgid());
        proContractAssist.setStaffName(proContractAssist.getStaffname());
        proContractAssist.setHrStaffApplyId(proContractAssist.getHrStaffApplyid());
        proContractAssist.setProCode(proCode);
        //添加项目名称
        proContractAssist.setProName(strCodes[2]);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String id = IdFactory.getUUIDUpper();
        proContractAssist.setId(id);
        proContractAssist.setAddBy(vo.getId());
        proContractAssist.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String codes = codeGeneratorConfigService.generatorCode("bpa_pro_contract");
        proContractAssist.setCodes(codes);
        // 处理processId
        proContractAssist.setProcessId(IdFactory.getUUIDUpper());
        proContractAssist.setProcessStatus("1");//审批中
        proContractAssist.setTypes("1");//PPP项目
        proContractAssist.setUptBy(vo.getId());
        proContractAssist.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        //合同投资表信息
        ProContractInvest proContractInvest = new ProContractInvest();
        String proContractInvestId = IdFactory.getUUIDUpper();//自动生成主键
        proContractInvest.setId(proContractInvestId);
        proContractInvest.setProContractId(id);
        proContractInvest.setAddBy(vo.getId());
        proContractInvest.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        //接收前端的值
        proContractInvest.setCaeFee(proContractAssist.getCaeFee());
        proContractInvest.setCaeDownRate(proContractAssist.getCaeDownRate());
        proContractInvest.setCaeRate(proContractAssist.getCaeRate());
        proContractInvest.setPrepareFee(proContractAssist.getPrepareFee());
        proContractInvest.setBuildOtherFee(proContractAssist.getBuildOtherFee());
        proContractInvest.setBuildOrgFee(proContractAssist.getBuildOrgFee());

        proContractInvest.setGdReplaceFee(proContractAssist.getGdReplaceFee());
        proContractInvest.setPreFee(proContractAssist.getPreFee());
        proContractInvest.setMoveFee(proContractAssist.getMoveFee());
        proContractInvest.setBuildInterest(proContractAssist.getBuildInterest());

        proContractInvest.setBuyFee(proContractAssist.getBuyFee());
        proContractInvest.setFlowFee(proContractAssist.getFlowFee());
        proContractInvest.setOutPreDownAmount(proContractAssist.getOutPreDownAmount());

        proContractInvest.setOutPreDownRate(proContractAssist.getOutPreDownRate());
        proContractInvest.setOutAfterDownAmount(proContractAssist.getOutAfterDownAmount());
        proContractInvest.setTotalSum(proContractAssist.getTotalSum());

        //公式信息
        List<ProContractUla> proContractUlas = new ArrayList<ProContractUla>();
        //公式类型
        String[] formulaTypes = proContractAssist.getFormulaTypes();
        if (formulaTypes != null && formulaTypes.length > 0) {
            for (int i = 0; i < formulaTypes.length; i++ ) {
                String formulaType = formulaTypes[i];
                String formulaMemo = proContractAssist.getFormulaMemos()[i];
                String paraMemo = proContractAssist.getParaMemos()[i];
                if (StringUtils.isEmpty(formulaType) && StringUtils.isEmpty(formulaType)) {
                    continue;
                }
                if (StringUtils.isNotEmpty(formulaMemo) || StringUtils.isNotEmpty(paraMemo)) {
                    ProContractUla proContractUla = new ProContractUla();
                    String proContractUlaId = IdFactory.getUUIDUpper();//自动生成主键
                    proContractUla.setId(proContractUlaId);
                    proContractUla.setProContractId(id);
                    proContractUla.setFormulaType(formulaType);
                    proContractUla.setFormulaMemo(formulaMemo);
                    proContractUla.setParaMemo(paraMemo);
                    proContractUla.setAddBy(vo.getId());
                    proContractUla.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
                    proContractUlas.add(proContractUla);
                }
            }
        }

        //新增合同清单分解子项信息
        List<ProContractSub> proContractSubs = new ArrayList<ProContractSub>();
        //合同清单分解子项费用明细
        List<ProContractDetail> proContractDetails = new ArrayList<ProContractDetail>();
        //子项名称
        String[] subNames = proContractAssist.getSubNames();
        if (subNames != null && subNames.length > 0) {
            for (int i = 0; i < subNames.length; i++ ) {
                String subName = subNames[i];
                String outPreDownAmountSub = proContractAssist.getOutPreDownAmountSubs()[i];
                String outPreDownRateSub = proContractAssist.getOutPreDownRateSubs()[i];
                String outAfterDownAmountSub = proContractAssist.getOutAfterDownAmountSubs()[i];
                if (StringUtils.isEmpty(subName)) {
                    continue;
                }
                ProContractSub proContractSub = new ProContractSub();
                String proContractSubId = IdFactory.getUUIDUpper();//自动生成主键
                proContractSub.setId(proContractSubId);
                proContractSub.setProContractId(id);
                proContractSub.setSubName(subName);
                proContractSub.setOutAfterDownAmount(new BigDecimal(outAfterDownAmountSub));
                proContractSub.setOutPreDownAmount(new BigDecimal(outPreDownAmountSub));
                proContractSub.setOutPreDownRate(new BigDecimal(outPreDownRateSub));
                proContractSub.setAddBy(vo.getId());
                Date addTime = top.infopub.mgr.util.DateUtils.addSeconds(
                    top.infopub.mgr.util.DateUtils.getCurrentDateTime(), i);
                proContractSub.setAddTime(addTime);
                proContractSubs.add(proContractSub);
            }

            //费用大类
            String[] feeTypes = proContractAssist.getFeeTypes();
            String[] feeSmallTypes = proContractAssist.getFeeSmallTypes();
            for (int j = 0; j < feeTypes.length; j++ ) {
                String feeType = feeTypes[j];
                String feeSmallType = feeSmallTypes[j];
                String itemNameSub = proContractAssist.getItemNameSub()[j];
                String typeName = proContractAssist.getTypeNames()[j];
                String orderIdx = proContractAssist.getOrderIdxs()[j];
                String outPreDownAmountDetial = proContractAssist.getOutPreDownAmountDetials()[j];
                String outPreDownRateDetial = proContractAssist.getOutPreDownRateDetials()[j];
                String outAfterDownAmountDetial = proContractAssist.getOutAfterDownAmountDetials()[j];
                if (StringUtils.isEmpty(feeType)) {
                    continue;
                }
                ProContractDetail proContractDetail = new ProContractDetail();
                String proContractDetailId = IdFactory.getUUIDUpper();//自动生成主键
                proContractDetail.setId(proContractDetailId);

                proContractDetail.setContractSubId(itemNameSub);
                proContractDetail.setProContractId(id);
                proContractDetail.setTypeName(typeName);
                proContractDetail.setOrderIdx(orderIdx);
                String[] split = orderIdx.split("\\.");
                if (split.length == 1) {
                    proContractDetail.setFeeSmallType(Integer.parseInt(feeSmallType));
                    proContractDetail.setFeeType(Integer.parseInt(feeType));
                    proContractDetail.setLevels(Integer.parseInt("1"));
                }
                else if (split.length == 2) {
                    String feeSmallType2 = feeSmallType.charAt(0) + "" + feeSmallType.charAt(2);
                    proContractDetail.setFeeSmallType(Integer.parseInt(feeSmallType2));
                    proContractDetail.setLevels(Integer.parseInt("2"));
                    char ch = orderIdx.charAt(0);
                    if (ch == '1') {
                        proContractDetail.setFeeType(1);
                    }
                    else if (ch == '2') {
                        proContractDetail.setFeeType(2);
                    }
                    else if (ch == '3') {
                        proContractDetail.setFeeType(3);
                    }
                    else if (ch == '4') {
                        proContractDetail.setFeeType(4);
                    }
                    else if (ch == '5') {
                        proContractDetail.setFeeType(5);
                    }
                    else if (ch == '6') {
                        proContractDetail.setFeeType(6);
                    }
                }
                else if (split.length == 3) {
                    String feeSmallType3 = feeSmallType.charAt(0) + "" + feeSmallType.charAt(2);
                    proContractDetail.setFeeSmallType(Integer.parseInt(feeSmallType3));
                    proContractDetail.setLevels(Integer.parseInt("3"));
                    char ch = orderIdx.charAt(0);
                    if (ch == '1') {
                        proContractDetail.setFeeType(1);
                    }
                    else if (ch == '2') {
                        proContractDetail.setFeeType(2);
                    }
                    else if (ch == '3') {
                        proContractDetail.setFeeType(3);
                    }
                    else if (ch == '4') {
                        proContractDetail.setFeeType(4);
                    }
                    else if (ch == '5') {
                        proContractDetail.setFeeType(5);
                    }
                    else if (ch == '6') {
                        proContractDetail.setFeeType(6);
                    }
                }
                proContractDetail.setOutAfterDownAmount(new BigDecimal(outAfterDownAmountDetial));
                proContractDetail.setOutPreDownAmount(new BigDecimal(outPreDownAmountDetial));
                proContractDetail.setOutPreDownRate(new BigDecimal(outPreDownRateDetial));
                proContractDetail.setAddBy(vo.getId());
                proContractDetail.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
                proContractDetails.add(proContractDetail);
            }

            for (ProContractDetail proContractDetail : proContractDetails) {
                for (ProContractSub proContractSub : proContractSubs) {
                    if (proContractDetail.getContractSubId().equals(proContractSub.getSubName())) {
                        proContractDetail.setContractSubId(proContractSub.getId());
                        break;
                    }
                }
            }

        }

        if (CollectionUtils.isEmpty(proContractDetails)) {
            return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("项目合同清单没有录入~");
        }

        //上传附件
        //1=Word版合同文本 
        List<ProContractAtt> proContractAtts = new ArrayList<ProContractAtt>();
        String[] fileAtt1 = proContractAssist.getFileAtts1();
        if (fileAtt1 != null) {
            for (String file : fileAtt1) {
                ProContractAtt proContractAtt = createFileAtt(vo, id, file);
                proContractAtt.setTypes("1");
                proContractAtts.add(proContractAtt);
            }
        }
        //2=PDF版合同文本
        String[] fileAtt2 = proContractAssist.getFileAtts2();
        if (fileAtt2 != null) {
            for (String file : fileAtt2) {
                ProContractAtt proContractAtt = createFileAtt(vo, id, file);
                proContractAtt.setTypes("2");
                proContractAtts.add(proContractAtt);
            }
        }
        //3=盖章版合同文本（选填）
        String[] fileAtt3 = proContractAssist.getFileAtts3();
        if (fileAtt3 != null) {
            for (String file : fileAtt3) {
                ProContractAtt proContractAtt = createFileAtt(vo, id, file);
                proContractAtt.setTypes("3");
                proContractAtts.add(proContractAtt);
            }
        }
        //4=其他（选填）
        String[] fileAtt4 = proContractAssist.getFileAtts4();
        if (fileAtt4 != null) {
            for (String file : fileAtt4) {
                ProContractAtt proContractAtt = createFileAtt(vo, id, file);
                proContractAtt.setTypes("4");
                proContractAtts.add(proContractAtt);
            }
        }
        proContractService.saveProContract(proContractAssist, proContractInvest, proContractUlas,
            proContractSubs, proContractDetails, proContractAtts);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "商务管理 -对外合同-项目合同：项目名="
                                                              + proContractAssist.getProName(),
            null);
        return RspVo.getSuccessResponse();
    }

    /**
     * 项目合同-补充签订
     * @throws ParseException 
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/sign")
    @ResponseBody
    public RspVo sign(HttpServletRequest request, ProContractVo proContractVo)
        throws ParseException {
        String[] fileAtt1 = request.getParameterValues("fileAtt1");
        String[] fileAtt2 = request.getParameterValues("fileAtt2");
        String[] fileAtt3 = request.getParameterValues("fileAtt3");
        String[] fileAtt4 = request.getParameterValues("fileAtt4");

        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String id = proContractVo.getId();
        List<ProContractAtt> proContractAtts = new ArrayList<ProContractAtt>();
        if (fileAtt1 != null) {
            for (String file : fileAtt1) {
                ProContractAtt proContractAtt = createFileAtt(vo, id, file);
                proContractAtt.setTypes("1");
                proContractAtts.add(proContractAtt);
            }
        }
        if (fileAtt2 != null) {
            for (String file : fileAtt2) {
                ProContractAtt proContractAtt = createFileAtt(vo, id, file);
                proContractAtt.setTypes("2");
                proContractAtts.add(proContractAtt);
            }
        }
        if (fileAtt3 != null) {
            for (String file : fileAtt3) {
                ProContractAtt proContractAtt = createFileAtt(vo, id, file);
                proContractAtt.setTypes("3");
                proContractAtts.add(proContractAtt);
            }
        }
        if (fileAtt4 != null) {
            for (String file : fileAtt4) {
                ProContractAtt proContractAtt = createFileAtt(vo, id, file);
                proContractAtt.setTypes("4");
                proContractAtts.add(proContractAtt);
            }
        }
        proContractService.sign(proContractVo, proContractAtts);
        LogUtil.bizLog(LogUtil.LogModelType.PROJECT, LogUtil.AcctionType.ADD,
            "商务管理 -对内合同-补充签订项目合同：项目名=" + proContractVo.getProName(), null);
        return RspVo.getSuccessResponse();
    }

    private ProContractAtt createFileAtt(AuthorizationVO vo, String proContractId, String file) {
        //资料信息数组
        String[] fileAttStr = file.split("\\|");
        ProContractAtt fileAtt = new ProContractAtt();
        fileAtt.setId(IdFactory.getUUIDSerialNumber());
        //fileAtt.setId(fileAttStr[2]);
        fileAtt.setProContractId(proContractId);//项目id
        String fileName = fileAttStr[1].substring(0, fileAttStr[1].lastIndexOf("."));
        String suffix = fileAttStr[1].substring(fileAttStr[1].lastIndexOf("."),
            fileAttStr[1].length()).toLowerCase();//后缀转小写
        fileAtt.setFileName(fileName);//文件名
        fileAtt.setFileSuffix(suffix);//文件后缀
        fileAtt.setFilePath(fileAttStr[3]);//文件保存路径
        fileAtt.setAddBy(vo.getId());//添加人
        fileAtt.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());//添加时间
        return fileAtt;
    }

    /**
     * 上传附件
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/uploadAtt")
    @ResponseBody
    public RspVo updateAtt(HttpServletRequest request) {
        List<String> returnFileStrList = new ArrayList<>();
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
        if (fileMap != null) {
            for (String name : fileMap.keySet()) {
                MultipartFile file = fileMap.get(name);
                //资料类型
                String types = request.getParameter("types");
                if (StringUtils.isEmpty(types)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
                }
                //上传文件
                String fileId = IdFactory.getUUIDSerialNumber();
                //服务器资源路径
                String serverPath = "proContract";
                String filePath = FileHelper.upload(file, serverPath);
                returnFileStrList.add("|" + file.getOriginalFilename().trim() + "|" + fileId + "|"
                                      + filePath);
                LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "上传对外合同-项目合同附件-文件名:"
                                                                      + filePath + "，文件路径:"
                                                                      + filePath, null);
            }
        }
        return RspVo.getSuccessResponse(returnFileStrList);
    }

    /**
     * 根据交底id查询相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
//    @RequestMapping("/getBringInInfoByDiscloseProId")
//    @ResponseBody
//    public Map<String, Object> getDiscloseProById(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        String proCodeAndCodes = request.getParameter("proCodeAndCodes");
//        String[] strCodes = proCodeAndCodes.split("\\_");
//        try {
//            if (StringUtils.isNotEmpty(proCodeAndCodes)) {
//                DiscloseProVo discloseProVo = discloseProService.selectById(strCodes[1]);//交底管理
//                CitySurvey citySurvey = citySurveyService.selectByPrimaryKey(discloseProVo.getSgCityId());//项目地址
//                DeciPro deciPro = deciProService.selectByProCode(strCodes[0]);//市场管理
//                OrgInfo orgInfo = new OrgInfo();
//                orgInfo.setProjectCode(strCodes[0]);
//                orgInfo.setState(1);
//                List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
//                OrgInfoDomain orgInfoVo = new OrgInfoDomain();
//                OrgInfoDomain parentOrgInfoVo = new OrgInfoDomain();
//                if (orgInfos.size() > 0) {
//                    orgInfoVo.setCompName(orgInfos.get(0).getCompName());
//                    orgInfoVo.setId(orgInfos.get(0).getId());
//                    OrgInfo parentOrgInfo = new OrgInfo();
//                    parentOrgInfo.setId(orgInfos.get(0).getParentId());
//                    parentOrgInfo.setState(1);
//                    List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
//                    if (parentOInfos.size() > 0) {
//                        parentOrgInfoVo.setCompName(parentOInfos.get(0).getCompName());
//                        parentOrgInfoVo.setId(parentOInfos.get(0).getId());
//                    }
//                }
//                //带入标后总结
//                Map<String, Object> whereMap = Maps.newHashMap();
//                whereMap.put("processStatus", "9");
//                whereMap.put("proCode", strCodes[0]);
//                whereMap.put("orderCondition", "ADD_TIME desc");
//                List<AfterSummary> afterSummaries = afterSummaryService.selectByMapWhere(whereMap);
//                if (CollectionUtils.isNotEmpty(afterSummaries)) {
//                    AfterSummary afterSummary = afterSummaries.get(0);
//                    map.put("afterSummary", afterSummary);
//                }
//                map.put("orgInfoVo", orgInfoVo);
//                map.put("parentOrgInfoVo", parentOrgInfoVo);
//                map.put("discloseProVo", discloseProVo);
//                map.put("citySurvey", citySurvey);
//                map.put("deciPro", deciPro);
//            }
//        }
//        catch (Exception e) {
//            log.error("查询交底/市场记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

    /**
     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 项目合同id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
        Map<String, Object> map = new HashMap<String, Object>();
        ProContractVo proContractVo = proContractService.queryByIds(id);
        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setProjectCode(proContractVo.getProCode());
        orgInfo.setState(1);
        List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
        if (orgInfos.size() > 0) {
            proContractVo.setOrgProName(orgInfos.get(0).getCompName());
            proContractVo.setHrOrgProId(orgInfos.get(0).getId());
            OrgInfo parentOrgInfo = new OrgInfo();
            parentOrgInfo.setId(orgInfos.get(0).getParentId());
            parentOrgInfo.setState(1);
            List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
            if (parentOInfos.size() > 0) {
                proContractVo.setOrgAreaName(parentOInfos.get(0).getCompName());
                proContractVo.setHrOrgAreaId(parentOInfos.get(0).getId());
            }
        }
        for (ProContractSubVo proContractSubVo : proContractVo.getProContractSubVos()) {
            // 其中： 对内下浮率如下 
            // 1. 带入项目【分包合同】（可能需查多个合同）子项对应{对内下浮率}
            // 2. 带入项目【内部协议】子项对应{对内下浮率}
            // 3. 若无对应子项，为0
            Map<String, Object> subpackCotWhere = Maps.newHashMap();
            subpackCotWhere.put("proCode", proContractVo.getProCode());
            subpackCotWhere.put("subName", proContractSubVo.getSubName());
            // 查询项目【分包合同】 子项对应{对内下浮率}
            BigDecimal rateFromSubpackCot = subpackCotDetailService.selectRateFromSubpackCot(subpackCotWhere);
            if (null == rateFromSubpackCot) {
                Map<String, Object> innerAgrtWhere = Maps.newHashMap();
                innerAgrtWhere.put("proCode", proContractVo.getProCode());
                innerAgrtWhere.put("subName", proContractSubVo.getSubName());
                // 查询项目【内部协议】子项对应{对内下浮率}
                BigDecimal rateFromInnerAgrt = innerAgrtDetailService.selectRateFromInnerAgrt(innerAgrtWhere);
                if (null == rateFromInnerAgrt) {
                    proContractSubVo.setInDownRates(BigDecimal.ZERO);
                }
                else {
                    proContractSubVo.setInDownRates(rateFromInnerAgrt);
                }
            }
            else {
                proContractSubVo.setInDownRates(rateFromSubpackCot);
            }
        }
        request.setAttribute("modelDetail", proContractVo);
        map.put("proContractVo", proContractVo);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, proContractVo.getProcessId());
        return new ModelAndView("bpa/lay-proContract-detail", map);
    }

    /**
     * 项目合同-复制单据
     * @return
     */
    @RequestMapping("/toCopy")
    public String toCopy(HttpServletRequest request, String id) {
        ProContractVo proContractVo = proContractService.queryByIds(id);
        request.setAttribute("proContract", proContractVo);
        return "bpa/lay-proContract-copy";
    }
    
    /**
     * 根据项目合同id查询相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getProContractById")
    @ResponseBody
    public Map<String, Object> getProContractById(HttpServletRequest request) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        String proCodeAndCodes = request.getParameter("proCodeAndCodes");
        String[] strCodes = proCodeAndCodes.split("\\_");
        try {
            ProContractVo proContractVo = proContractService.queryByIds(strCodes[1]);
            map.put("proContractVo", proContractVo);
        }
        catch (Exception e) {
            log.error("查询项目合同记录异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }

}
